<template>
  <!-- 表单 -->
  <div class="box">
    <el-form ref="addForm" label-width="80px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 100%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 100%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 100%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model.number="formData.formOfEmployment"
          style="width: 100%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in shape"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 100%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 100%"
          placeholder="请选择部门"
          @focus="showTree = true"
        />
        <div v-if="showTree">
          <el-tree
            default-expand-all
            :data="list"
            :props="defaultProps"
            @node-click="handleNodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 100%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
      <el-form-item style="margin-left: 80px">
        <el-button @click="hCancel">取消</el-button>
        <el-button type="primary" @click.native="hSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { validMobile } from '@/utils/validate' // 引入校验手机号的函数
import EmployeeEnum from '@/api/constant/employees'
import { getDepartmentApi } from '@/api/departments'
import { addEmployeeApi } from '@/api/employees'
import arrayToTree from 'array-to-tree'
export default {
  name: '',
  data() {
    // 自定义校验函数
    const validateMobile = function(rule, value, callback) {
      // 校验value
      if (validMobile(value)) {
        callback()
      } else {
        callback(new Error('输入的手机号格式不正确'))
      }
    }
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' }
        ]
      },
      shape: EmployeeEnum.hireType,
      list: [],
      formOfEmployment: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showTree: false
    }
  },
  computed: {},
  created() {
    this.getDepartment()
  },
  methods: {
    hCancel() {},
    // 提交表单
    async hSubmit() {
      this.$refs.addForm.validate((valid) => {
        if (!valid) return false
      })
      await addEmployeeApi(this.formData)
      this.$emit('success')
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      this.$refs.addForm.resetFields()
      this.$notify({
        title: '成功',
        message: '添加角色数据成功!',
        type: 'success'
      })
    },
    /* 获取部门 */
    async getDepartment() {
      const res = await getDepartmentApi()
      this.list = arrayToTree(res.data.depts, {
        parentProperty: 'pid',
        customID: 'id'
      })
    },
    // 点击子部门触发
    handleNodeClick(data) {
      console.log(data)
      if (data.children && data.children.length) {
        return this.$message.info('请选择子部门')
      } else {
        this.showTree = false
        return (this.formData.departmentName = data.name)
      }
    }
  }
}
</script>
<style lang="scss" scoped></style>
